home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / hplip / print.py < prev    next >
Text File  |  2008-10-13  |  5KB  |  207 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '4.0'
  24. __title__ = 'Print Utility'
  25. __doc__ = "A simple front end to 'lp'. Provides a print UI from the Device Manager if kprinter, gtklp, or xpp are not installed."
  26.  
  27. # Std Lib
  28. import sys
  29. import os
  30. import getopt
  31.  
  32. # Local
  33. from base.g import *
  34. from base import utils, device, tui
  35. from prnt import cups
  36.  
  37. log.set_module('hp-print')
  38.  
  39. app = None
  40. printdlg = None
  41.  
  42. USAGE = [(__doc__, "", "name", True),
  43.          ("Usage: hp-print [PRINTER|DEVICE-URI] [OPTIONS] [FILE LIST]", "", "summary", True),
  44.          utils.USAGE_ARGS,
  45.          utils.USAGE_DEVICE,
  46.          ("To specify a CUPS printer:", "-P<printer>, -p<printer> or --printer=<printer>", "option", False),
  47.          utils.USAGE_SPACE,
  48.          utils.USAGE_OPTIONS,
  49.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  50.          utils.USAGE_HELP,
  51.          ("[FILELIST]", "", "heading", False),
  52.          ("Optional list of files:", """Space delimited list of files to print. Files can also be selected for print by adding them to the file list in the UI.""", "option", False),
  53.          utils.USAGE_SPACE,
  54.          utils.USAGE_NOTES,
  55.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  56.          ]
  57.  
  58.  
  59. def usage(typ='text'):
  60.     if typ == 'text':
  61.         utils.log_title(__title__, __version__)
  62.  
  63.     utils.format_text(USAGE, typ, __title__, 'hp-print', __version__)
  64.     sys.exit(0)
  65.  
  66.  
  67. try:
  68.     opts, args = getopt.getopt(sys.argv[1:], 'P:p:d:hl:gq:',
  69.                                ['printer=', 'device=', 'help', 
  70.                                 'help-rest', 'help-man', 'logging=', 
  71.                                 'lang=', 'help-desc'])
  72. except getopt.GetoptError, e:
  73.     log.error(e.msg)
  74.     usage()
  75.  
  76. printer_name = None
  77. device_uri = None
  78. log_level = logger.DEFAULT_LOG_LEVEL
  79. loc = None
  80. bus = ['cups']
  81.  
  82. if os.getenv("HPLIP_DEBUG"):
  83.     log.set_level('debug')
  84.  
  85. for o, a in opts:
  86.     if o in ('-h', '--help'):
  87.         usage()
  88.  
  89.     elif o == '--help-rest':
  90.         usage('rest')
  91.  
  92.     elif o == '--help-man':
  93.         usage('man')
  94.  
  95.     elif o == '--help-desc':
  96.         print __doc__,
  97.         sys.exit(0)
  98.  
  99.     elif o in ('-p', '-P', '--printer'):
  100.         if a.startswith('*'):
  101.             printer_name = cups.getDefaultPrinter()
  102.             log.debug(printer_name)
  103.             
  104.             if printer_name is not None:
  105.                 log.info("Using CUPS default printer: %s" % printer_name)
  106.             else:
  107.                 log.error("CUPS default printer is not set.")
  108.             
  109.         else:
  110.             printer_name = a
  111.  
  112.     elif o in ('-d', '--device'):
  113.         device_uri = a
  114.  
  115.     elif o in ('-l', '--logging'):
  116.         log_level = a.lower().strip()
  117.         if not log.set_level(log_level):
  118.             usage()
  119.  
  120.     elif o == '-g':
  121.         log.set_level('debug')
  122.  
  123.     elif o in ('-q', '--lang'):
  124.         if a.strip() == '?':
  125.             tui.show_languages()
  126.             sys.exit(0)
  127.  
  128.         loc = utils.validate_language(a.lower())
  129.  
  130.  
  131.  
  132. # Security: Do *not* create files that other users can muck around with
  133. os.umask (0037)
  134.  
  135. utils.log_title(__title__, __version__)
  136.  
  137. if os.getuid() == 0:
  138.     log.warn("hp-print should not be run as root.")
  139.  
  140. if not utils.canEnterGUIMode():
  141.     log.error("hp-print requires GUI support. Exiting.")
  142.     sys.exit(1)
  143.  
  144.  
  145. from qt import *
  146. from ui.printerform import PrinterForm
  147.  
  148.  
  149. # create the main application object
  150. app = QApplication(sys.argv)
  151.  
  152. if loc is None:
  153.     loc = user_cfg.ui.get("loc", "system")
  154.     if loc.lower() == 'system':
  155.         loc = str(QTextCodec.locale())
  156.         log.debug("Using system locale: %s" % loc)
  157.  
  158. if loc.lower() != 'c':
  159.     e = 'utf8'
  160.     try:
  161.         l, x = loc.split('.')
  162.         loc = '.'.join([l, e])
  163.     except ValueError:
  164.         l = loc
  165.         loc = '.'.join([loc, e])
  166.  
  167.     log.debug("Trying to load .qm file for %s locale." % loc)
  168.     trans = QTranslator(None)
  169.  
  170.     qm_file = 'hplip_%s.qm' % l
  171.     log.debug("Name of .qm file: %s" % qm_file)
  172.     loaded = trans.load(qm_file, prop.localization_dir)
  173.  
  174.     if loaded:
  175.         app.installTranslator(trans)
  176.     else:
  177.         loc = 'c'
  178.  
  179. if loc == 'c':
  180.     log.debug("Using default 'C' locale")
  181. else:
  182.     log.debug("Using locale: %s" % loc)
  183.     QLocale.setDefault(QLocale(loc))
  184.     prop.locale = loc
  185.     try:
  186.         locale.setlocale(locale.LC_ALL, locale.normalize(loc))
  187.     except locale.Error:
  188.         pass
  189.  
  190.  
  191. printdlg = PrinterForm(bus, device_uri, printer_name, args)
  192. printdlg.show()
  193. app.setMainWidget(printdlg)
  194.  
  195. try:
  196.     log.debug("Starting GUI loop...")
  197.     app.exec_loop()
  198. except KeyboardInterrupt:
  199.     pass
  200. #except:
  201. #    log.exception()
  202.  
  203. #sock.close()
  204. sys.exit(0)
  205.  
  206.  
  207.